package com.hefu.virtuallife.ui.purchase;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dz.utlis.JavaUtils;
import com.hefu.virtuallife.R;
import com.hefu.virtuallife.baseui.BaseActivity;
import com.hefu.virtuallife.utils.ToastUtils;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.uuzuche.lib_zxing.activity.CaptureFragment;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import io.reactivex.functions.Consumer;
import rx.functions.Action1;

/**
 * creat_user: zhengzaihong
 * email:1096877329@qq.com
 * creat_date: 2018/8/13
 * creat_time: 18:29
 * describe: 扫描界面
 **/
public class ScanCodeActivity extends BaseActivity {

    private static final String TAG = ScanCodeActivity.class.getSimpleName();

    private ImageButton iv_back;
    TextView tv_flashlight;
    private CaptureFragment captureFragment;

    @Override
    protected void initData() {
        super.initData();

        iv_back = findViewById(R.id.iv_back);
        iv_back.setOnClickListener(view -> finish());

        tv_flashlight = findViewById(R.id.tv_flashlight);
        tv_flashlight.setOnClickListener(v -> setIsOpen());


        getPermission();
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_scan_code;
    }


    private void getPermission() {
      /*  RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.requestEach(this, Manifest.permission.CAMERA)*/

        new RxPermissions(this)
                .requestEach(Manifest.permission.CAMERA)
                .subscribe(permission -> {
                    if (permission.granted) {
                        // 获得授权
                        captureFragment = new CaptureFragment();
                        // 为二维码扫描界面设置定制化界面
                        CodeUtils.setFragmentArgs(captureFragment, R.layout.my_camera);
                        captureFragment.setAnalyzeCallback(analyzeCallback);
                        getSupportFragmentManager().beginTransaction().replace(R.id.fl_my_container, captureFragment).commit();

                    } else {
                        this.finish();
                        //   isGrantedAll = false;
                        // Log.d(TAG,"i///sGrantedAll = " +isGrantedAll);
                        //未获得授权
                        ToastUtils.getInstance().show("您没有授权该权限，请在设置中打开授权");
                    }
                });

    }


    /**
     * 二维码解析回调函数
     */
    CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() {
        @Override
        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_SUCCESS);
            bundle.putString(CodeUtils.RESULT_STRING, result);
            resultIntent.putExtras(bundle);

            ToastUtils.getInstance().show(result);
            JavaUtils.outPrint(result);
            // ScanCodeActivity.this.setResult(RESULT_OK, resultIntent);
            //ScanCodeActivity.this.finish();
        }

        @Override
        public void onAnalyzeFailed() {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_FAILED);
            bundle.putString(CodeUtils.RESULT_STRING, "");
            resultIntent.putExtras(bundle);
            ScanCodeActivity.this.setResult(RESULT_OK, resultIntent);
            ScanCodeActivity.this.finish();
        }
    };

    public Boolean isOpen = false;

    public void setIsOpen() {
        if (isOpen == false) {
            CodeUtils.isLightEnable(true);
            Drawable top = getResources().getDrawable(R.drawable.flashlight_selected);
            tv_flashlight.setCompoundDrawablesWithIntrinsicBounds(null, top, null, null);
            tv_flashlight.setText("轻触关闭");
            isOpen = true;
        } else {
            CodeUtils.isLightEnable(false);
            Drawable top = getResources().getDrawable(R.drawable.flashlight);
            tv_flashlight.setCompoundDrawablesWithIntrinsicBounds(null, top, null, null);
            tv_flashlight.setText("轻触照亮");
            isOpen = false;
        }
    }
}